package io.github.talelin.latticy.controller.v1;

import io.github.talelin.core.annotation.*;
import io.github.talelin.latticy.dto.warearea.UpdateWareareaDTO;
import io.github.talelin.latticy.dto.warearea.WareareaDTO;
import io.github.talelin.latticy.service.WareareaService;
import io.github.talelin.latticy.vo.WareareaVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.model.WareareaDO;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Positive;
import java.util.List;

/**
 * 仓库分区前端控制器
 *
 * @author generator@wulongbo
 * @since 2022-06-07
 */
@RestController
@RequestMapping("/v1/warearea")
@Validated
@PermissionModule(value = "分区管理")
public class WareareaController {

    @Autowired
    private WareareaService wareareaService;

    @PostMapping("")
    @GroupRequired
    @PermissionMeta(value = "创建分区")
    @Logger(template = "{user.username}创建分区:{request.name}")
    public CreatedVO create(@RequestBody @Validated WareareaDTO validator) {
        wareareaService.createWareArea(validator);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "更新分区")
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id, @RequestBody @Validated UpdateWareareaDTO dto) {
        wareareaService.updateWareArea(id, dto);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除分区")
    @Logger(template = "{user.username}删除分区:{request.wareareaMapper-name}")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        wareareaService.deleteWareArea(id);
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    @LoginRequired
    public WareareaDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        return wareareaService.getWareAreaById(id);
    }

    @GetMapping("/list/{code}")
    @GroupRequired
    @PermissionMeta(value = "查询分区")
    public List<WareareaVO> list(@PathVariable(value = "code") @NotBlank(message = "{warehouse.code.not-blank}") String code) {
        return wareareaService.getWareAreaList(code);
    }

}
